package org.lifxue.jqda.ui;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lifxue.jqda.ui.util.UIUtil;
import org.lifxue.jqda.util.GlobalConstant;
import org.lifxue.jqda.util.GlobalVar;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.SubstanceSkin;
import org.pushingpixels.substance.api.skin.SkinInfo;

/**
 * 
 * @Description:
 * @Author: zhangzuoqiang
 * @Date: Sep 26, 2011
 */
public class LookAndFeelJDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Log logger = LogFactory.getLog(LookAndFeelJDialog.class);
	private static LookAndFeelJDialog LAF_JDIALOG;

	public static LookAndFeelJDialog getInstance(boolean modal) {
		synchronized (LookAndFeelJDialog.class) {
			if (LAF_JDIALOG == null) {
				LAF_JDIALOG = new LookAndFeelJDialog(modal);
			}
		}
		return LAF_JDIALOG;
	}

	public LookAndFeelJDialog(boolean modal) {
		super(new JFrame(), modal);
		initComponents();

		// 窗口定位
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		setLocation(new Point((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.width) / 2));
	}

	/**
	 * Creates new form LookAndFeelJDialog
	 * 
	 * @param parent
	 * @param modal
	 */
	public LookAndFeelJDialog(Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">
	private void initComponents() {

		jComboBox1 = new JComboBox();
		jLabel1 = new JLabel();
		jButton1 = new JButton();

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setTitle("界面风格设置");
		setIconImage(Toolkit.getDefaultToolkit().createImage(
				getClass().getResource("/org/lifxue/jqda/ui/butterfly16.png")));

		addComBoxItem();
		jComboBox1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent evt) {
				jComboBox1ItemStateChanged(evt);
			}
		});

		jLabel1.setText("选择界面风格:");

		jButton1.setText("关闭");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jLabel1)
																				.addComponent(
																						jComboBox1,
																						GroupLayout.PREFERRED_SIZE,
																						172,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(66, 66,
																		66)
																.addComponent(
																		jButton1)))
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jComboBox1,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED,
										23, Short.MAX_VALUE)
								.addComponent(jButton1).addContainerGap()));
		pack();
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		this.setVisible(false);
		dispose();
	}

	private void jComboBox1ItemStateChanged(ItemEvent evt) {
		if (evt.getStateChange() == ItemEvent.SELECTED) {
			String lafName = evt.getItem().toString();
			setUI(lafName);
		}
	}

	private void addComBoxItem() {
		String LookAndFeelDefault = "";
		SubstanceSkin currentSkin = SubstanceLookAndFeel.getCurrentSkin();
		if (currentSkin != null) {
			LookAndFeelDefault = currentSkin.getDisplayName();
		}

		// substance
		Map<String, SkinInfo> hm = SubstanceLookAndFeel.getAllSkins();
		Iterator<Entry<String, SkinInfo>> skins = hm.entrySet().iterator();
		while (skins.hasNext()) {
			Entry<String, SkinInfo> entry = (Entry<String, SkinInfo>) skins
					.next();
			String key = (String) entry.getKey(); // 返回与此项对应的键
			SkinInfo in = (SkinInfo) entry.getValue();
			logger.info(in.getClassName());
			jComboBox1.addItem(key);
			if (LookAndFeelDefault.equals(key)) {
				jComboBox1.setSelectedItem(key);
			}
		}

	}

	private void setUI(String lafName) {
		// sunstance
		Map<String, SkinInfo> hm = SubstanceLookAndFeel.getAllSkins();
		Iterator<Entry<String, SkinInfo>> skins = hm.entrySet().iterator();
		while (skins.hasNext()) {
			Entry<String, SkinInfo> entry = (Entry<String, SkinInfo>) skins
					.next();
			SkinInfo skinInfo = (SkinInfo) entry.getValue();

			if (lafName.equals(skinInfo.getDisplayName())) {
				GlobalVar.CONFIG.put(GlobalConstant.CONFIG_LOOK_AND_FEEL,
						skinInfo.getClassName());
				UIUtil.setUI();
				return;
			}
		}

	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				LookAndFeelJDialog dialog = new LookAndFeelJDialog(
						new JFrame(), true);
				dialog.addWindowListener(new WindowAdapter() {

					@Override
					public void windowClosing(WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	private JButton jButton1;
	private JComboBox jComboBox1;
	private JLabel jLabel1;
}
